import { defineStore } from "pinia";
import http from "../utils/request";
export const useMenusStore = defineStore("menus", {
    state: () => {
        return {
            /**
			 * [{
				label: "首页",
				path: "/"
			}, {
				label: "测试",
				path: "test",
				children: [{
					label: '测试1',
					path: '/test/test1',
				}, {
					label: '测试测试测试2',
					path: '/test/test2',
				}]
			}, {
				label: "运维",
				path: "maintenance",
				children: [{
					label: '后台菜单',
					path: '/maintenance/menus',
				}, {
					label: '数据字典',
					path: '/maintenance/dataDictionary'
				}, {
					label: '附件管理',
					path: '/maintenance/attachment'
				}]
			}, ]
			 */
            navigationMenusList: [], //过滤后可供导航组件显示的菜单数据
            menuOriginData: [], //完整原始的menus数据,一般在管理员角色组权限选择处使用
            currentLevel1MenuKey: "",
            currentLevel2MenuKey: "", //当前活动的菜单页面key
        };
    },
    persist: true,

    getters: {},
    actions: {
        //处理后端来的菜单数据
        handleAccountMenus(AccountMenus) {
            this.menuOriginData = AccountMenus;
            let filterTree = (tree) => {
                if (!Array.isArray(tree)) return [];

                return tree
                    .filter((node) => ["menu_group", "route"].includes(node.type))
                    .map((node) => {
                        node.label = node.name;
                        node.path = node.route_path;
                        return {
                            ...node,
                            children: node.children ? filterTree(node.children) : [],
                        };
                    });
            };

            this.navigationMenusList = filterTree(AccountMenus);
            console.log(this.navigationMenusList);
        },
        //超管获取所有菜单数据
        getAllMenus() {
            return http.get("/sunAdmin/menus/getAllMenus");
        },
        editMenuItem(data) {
            return http.post("/sunAdmin/menus/edit", data);
        },
        delMenu(menuId) {
            return http.get("/sunAdmin/menus/deleteMenus/" + menuId);
        },
    },
});
